package com.lmc.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lmc.R;
import com.lmc.entity.Health;
import com.lmc.util.DateFormatUtil;
import com.lmc.util.GlideUtil;

import java.util.List;

public class HealthListAdapter extends BaseAdapter {
    List<Health.DataBean.ResultBean> list;
    Context context;
    int isDrag;

    public HealthListAdapter(List<Health.DataBean.ResultBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public int getIsDrag() {
        return isDrag;
    }

    public void setIsDrag(int isDrag) {
        this.isDrag = isDrag;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Health.DataBean.ResultBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.health_list_item, null);
            holder = new ViewHolder();
            holder.name = convertView.findViewById(R.id.name);
            holder.num = convertView.findViewById(R.id.num);
            holder.value = convertView.findViewById(R.id.value);
            holder.time = convertView.findViewById(R.id.time);
            holder.icon = convertView.findViewById(R.id.icon);
            holder.line_layout = convertView.findViewById(R.id.line_layout);
            holder.jt = convertView.findViewById(R.id.jt);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Health.DataBean.ResultBean item = getItem(position);
        holder.icon.setImageResource(item.getId());

        holder.name.setText(item.getName());

        if (isDrag == 0) {
            holder.line_layout.setVisibility(View.INVISIBLE);
            holder.jt.setVisibility(View.VISIBLE);
        } else {
            holder.jt.setVisibility(View.INVISIBLE);
            holder.line_layout.setVisibility(View.VISIBLE);
        }

        holder.icon.setImageResource(item.getId());
        if (item.getName().equals("每日健康报告")) {
            holder.num.setVisibility(View.GONE);
            holder.value.setVisibility(View.GONE);
            holder.time.setVisibility(View.GONE);

        } else {
            holder.num.setVisibility(View.VISIBLE);
            holder.value.setVisibility(View.VISIBLE);
            holder.time.setVisibility(View.VISIBLE);
            holder.num.setText(item.getValue());
            holder.value.setText(item.getUnit());
            if (item.getDate() == 0) {
                holder.time.setText("");
                holder.time.setVisibility(View.GONE);
            } else {

                holder.time.setText("测量于" + DateFormatUtil.forString(item.getDate(), "yyyy.MM.dd   HH:mm"));
            }
        }
        return convertView;
    }

    public class ViewHolder {
        ImageView icon, jt;
        TextView name, num, value, time;
        LinearLayout line_layout;
    }
}
